const app = getApp();
Page({
  data: {
    config: app.config,
    
    imgUrl: '',
    promotrUrl: ''
  },
  onLoad() {
    my.setNavigationBar({
      reset: true
    });


    this.erweiFun();


  },



  erweiFun(){
    const appIdRes = my.getAppIdSync();
    let appid = appIdRes.appId;
    let user = my.getStorageSync({ key: 'user' }).data;
    console.log(55,user);
    if(!user){
      my.showToast({
        content: '请先登录！'
      })
      return;
    }

    my.showLoading({
      content:'生成中'
    });

    let promoteId = user.promoteId ? user.promoteId : '';
    let promoteUserId = user.promoteUserId ? user.promoteUserId : '';
    let codeUrl = `https://ds.alipay.com/?scheme=`+ encodeURIComponent(`alipays://platformapi/startapp?appId=${appid}&page=pages/main/home/home&query=${encodeURIComponent(`promoteUserId=${promoteUserId}&promoteId=${promoteId}`)}`)

    let paramCon = `promoteUserId=${promoteUserId}&promoteId=${promoteId}`
    let dat = {
      url_param: 'pages/main/home/home',
      query_param: paramCon,
      describe: '推广分享',
      size: 's'
    }

    app.user.QRCode(dat)
     .then(res=>{
       if(res.data){
         let con = JSON.parse(res.data);
         console.log(22,con);
         this.setData({
          imgUrl: con.alipay_open_app_qrcode_create_response.qr_code_url,
          promotrUrl: codeUrl
        });
       }else{
        my.showToast({
          content: '二维码生成失败'
        })
       }
       my.hideLoading();
     })
     .catch(err=>{
        my.showToast({
          content: '二维码生成失败'
        })
        my.hideLoading();
     })

    // let promoteId = user.promoteId ? user.promoteId : '';
    // let promoteUserId = user.promoteUserId ? user.promoteUserId : '';
    // let codeUrl = `https://ds.alipay.com/?scheme=`+ encodeURIComponent(`alipays://platformapi/startapp?appId=${appid}&page=pages/main/home/home&query=${encodeURIComponent(`promoteUserId=${promoteUserId}&promoteId=${promoteId}`)}`)
    
    // if (my.canIUse('generateImageFromCode')) {
    //   my.generateImageFromCode({
    //     code: codeUrl,
    //     format: 'QRCODE',
    //     width: 310,
    //     correctLevel: 'H',
    //     success: res => {
    //       console.log(666,res.image);
    //       this.setData({
    //         imgUrl: res.image,
    //         promotrUrl: codeUrl
    //       });
    //     },
    //     fail(res) {
    //       my.showToast({
    //         content: '二维码生成失败'
    //       })
    //     }
    //   });
    // }
  },



  copyBtn(){
    my.setClipboard({
      text: this.data.promotrUrl,
      success:res=>{
        my.showToast({
          content: '复制成功'
        })
      }
    });
  }


});
